/*
 * AbstractWeapon.java
 *
 * Created on 28. April 2007, 11:27
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tor.obj;

import com.golden.gamedev.GameObject;
import de.tor.obj.impl.ProjectileFactory;
import java.awt.geom.AffineTransform;

/**
 *
 * @author Charon
 */
public abstract class AbstractWeapon{
    
    private int iShotsPerSecond = 0;
    private int lCooldownTime = 0;
   
    private double dXPos = 0;
    private double dYPos = 0;
    
    private long iCooledTime = 0;
    private TorridityGameObject mParent;
    
    private int iCurrentProjectile = ProjectileFactory.NO_PROJECTILE;
    
    private double dRotation = 0;
    
    /** Creates a new instance of AbstractWeapon */
    public AbstractWeapon(TorridityGameObject parent) {
        mParent = parent;
    }
    
    public void setShotsPerSecond(int v){
        iShotsPerSecond  = v;
        lCooldownTime = (int)(1000/v);
    }
    
    public int getShotsPerSecond(){
        return iShotsPerSecond;
    }
    
    public void setRotation(double v){
        dRotation = v;
    }
    
    public double getRotation(){
        return dRotation;
    }
    
    public TorridityGameObject getParent(){
        return mParent;
    }
    
    public abstract AbstractProjectile createProjectile();
    
    private boolean isCooledDown(){
        return (iCooledTime <= 0);
    }
    
    public void setObjectRelativePosition(double x, double y){
        dXPos = x;
        dYPos = y;
    }
    
    public void setProjectile(int v){
        iCurrentProjectile = v;
        setShotsPerSecond(ProjectileFactory.getShotsPerSecond(v));
    }
    
    public int getProjectile(){
        return iCurrentProjectile;
    }
    
    public AbstractProjectile fireShot(long elapsedTime){
        if(!isCooledDown()){
            iCooledTime -= elapsedTime;
            return null;
        }
        iCooledTime = lCooldownTime;
        AbstractProjectile pro = createProjectile();
        pro.setupProjectile();
        double rad = -getRotation() / (360 / (2*Math.PI));
        double xPos = (dXPos * Math.cos(rad)) - (dYPos * Math.sin(rad));
        double yPos = (dYPos * Math.cos(rad)) + (dXPos * Math.sin(rad));
        pro.setLocation(getParent().getX() + getParent().getWidth()/2 + xPos - pro.getWidth()/2, getParent().getY() + getParent().getHeight()/2 - yPos);
        pro.setRotation(dRotation);
        return pro;
    }
    
}
